<?php

namespace App\Http\Controllers\Admin\Pages;

use App\Facades\ChargeRecordService;
use App\Facades\DeviceService;
use App\Facades\DeviceInfoService;
use App\Facades\UserService;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Utils\ExportExcel;
use Cache;
use DB;
use Carbon\Carbon;
use Illuminate\Http\Request;

class DeviceController extends Controller
{
    //设备列表
    public function index(Request $request)
    {
        switch ($request->status) {
            case '缺水':
                $status = \App\Models\DeviceRecord::HYDROPENIA;
                break;
            case '检修':
                $status = \App\Models\DeviceRecord::OVERHAUL;
                break;
            case '漏水':
                $status = \App\Models\DeviceRecord::LEAKAGE;
                break;
            case '停机':
                $status = \App\Models\DeviceRecord::HALT;
                break;
            default:
                $status = '';
                break;
        }
        $condition = $this->formatCondition($request->condition);

        $deviceList = DeviceService::findDeviceRecordLlist($condition, $status);
        $price = ChargeRecordService::getPrice();

        return view("admin/deviceList")->with([
            'device_list' => $deviceList,
            'condition' => $request->condition,
            'status' => $request->status,
            'price' => $price,
            ]);
    }

    //设备历史记录
    public function history(Request $request)
    {
        $rules = [
            'device_id' => 'required|exists:devices,id',
        ];
        $this->validate($request, $rules);
        $deviceRecords = DeviceService::findDeviceRecordListByDeviceCode($request->device_id);
        // dd($deviceRecords);
        if(count($deviceRecords)){
            $device = $deviceRecords->first()->device;
            if(!is_null($device)){
                $installInfo = DeviceInfoService::getInstallInfoByDeviceCode($device->code);
            }
        }
        return view("admin/history")->with([
            'device_records' => $deviceRecords,
            'install_info' => $installInfo,
            ]);
    }

    //设备流量提醒列表
    public function flowRemindList(Request $request)
    {
        $condition = $this->formatCondition($request->condition);
        $remindRecords = DeviceService::findFlowRemindList($condition);
        return view("admin/remindersFlux")->with([
            'remind_records' => $remindRecords,
            'condition' => $request->condition,
            ]);
    }

    //设备滤芯提醒列表
    public function fliterRemindList(Request $request)
    {
        $condition = $this->formatCondition($request->condition);
        $remindRecords = DeviceService::findFliterRemindList($condition);
        return view("admin/reminders")->with([
            'remind_records' => $remindRecords,
            'condition' => $request->condition,
            ]);
    }

    //安装信息列表
    public function installInfoList(Request $request)
    {
        $condition = $this->formatCondition($request->condition);
        $installInfoList = DeviceInfoService::findInstallInfoList($condition);
        return view("admin/installInfos")->with([
            'install_infos' => $installInfoList,
            'condition' => $request->condition,
            ]);
    }

    //维护信息列表
    public function maintainInfoList(Request $request)
    {
        $condition = $this->formatCondition($request->condition);
        $maintainInfoList = DeviceInfoService::findMaintainInfoList($condition);
        return view("admin/maintainInfos")->with([
            'maintain_infos' => $maintainInfoList,
            'condition' => $request->condition,
            ]);
    }
    //维护信息历史记录
    public function findmaintainInfoByCustomerName(Request $request)
    {
        $rules = [
            'customer_name' => 'required|exists:maintain_infos,customer_name',
        ];
        $this->validate($request, $rules);

        $maintainInfoList = DeviceInfoService::findmaintainInfoByCustomerName($request->customer_name);
        return view("admin/maintainInfosNotes")->with([
            'maintain_infos' => $maintainInfoList,
            'condition' => $request->device_code,
            ]);
    }

    //流量充值
    public function chargeList(Request $request)
    {
        $condition = $request->condition;
        $condition = str_replace('%', '\%', $condition);
        $condition = str_replace('_', '\_', $condition);
        $chargeList = ChargeRecordService::findChargeList($condition);
        $price = ChargeRecordService::getPrice();
        return view("admin/chargeRecords")->with([
            'charge_records' => $chargeList,
            'condition' => $request->condition,
            'price' => $price,
            ]);
    }

    //流量充值历史记录
    public function chargeHistory(Request $request)
    {
        $rules = [
            'device_id' => 'required|exists:devices,id'
        ];
        $this->validate($request, $rules);

        $chargeList = ChargeRecordService::getChargeListByDeviceId($request->device_id);
        $price = ChargeRecordService::getPrice();
        return view("admin/chargeRecordsNotes")->with([
            'charge_records' => $chargeList,
            ]);
    }
    //经销商管理
    public function chargeListAboutDealer(Request $request)
    {
        $rules = [
            'after_date' => 'required_with:before_date|date_format:Y-m-d',
            'before_date' => 'required_with:after_date|date_format:Y-m-d',
        ];
        $this->validate($request, $rules);

        $chargeList = ChargeRecordService::getChargeListByDateAndDealer($request->after_date, $request->before_date, $request->dealer);
        if($request->has('after_date')){
            $after_date = Carbon::createFromFormat('Y-m-d', $request->after_date);
            $before_date = Carbon::createFromFormat('Y-m-d', $request->before_date);
        }else{
            $after_date = $request->after_date;
            $before_date = $request->before_date;
        }
        return view('admin/dealer')->with([
                'after_date' => $after_date,
                'before_date' => $before_date,
                'dealer' => $request->dealer,
                'charge_records' => $chargeList,
            ]);
    }

    public function chargeListAboutDealerExport(Request $request)
    {
        $rules = [
            'after_date' => 'required_with:before_date|date_format:Y-m-d',
            'before_date' => 'required_with:after_date|date_format:Y-m-d',
        ];
        $this->validate($request, $rules);

        UserService::hasPermission(['superadmin', 'admin', 'finance']);
        $chargeList = ChargeRecordService::getChargeListByDateAndDealer($request->after_date, $request->before_date, $request->dealer, false);

        $formatChargeArray = [];
        foreach ($chargeList as $key => $charge) {
            $_charge['序号'] = $key+1;
            $_charge['经销商'] = strval(is_null($charge->device->installInfo)? '': $charge->device->installInfo->dealer);
            $_charge['设备编号'] = strval($charge->device->code);
            $_charge['用户名'] = strval(is_null($charge->device->installInfo)? '': $charge->device->installInfo->customer_name);
            $_charge['剩余流量(L)'] = is_null($charge->residual_flow)? '': $charge->residual_flow;
            $_charge['剩余天数(天)'] = is_null($charge->time_limit)? '': $charge->time_limit;
            $_charge['充值模式'] = ($charge->recharge_mode == 1)? '计量模式': '套餐模式';
            $_charge['购买金额(元)'] = is_null($charge->money)? 0: $charge->money;
            $_charge['购买流量数(L)'] = is_null($charge->flow)? : $charge->flow;
            $_charge['购买天数(天)'] = is_null($charge->days)? : $charge->days;
            $_charge['购买人'] = strval(is_null($charge->userInfo)? '': $charge->userInfo->name);
            $_charge['后台充值人'] = strval(is_null($charge->adminInfo)? '': $charge->adminInfo->name);
            $_charge['撤销金额(元)'] = strval(is_null($charge->discharge_money)? '': $charge->discharge_money);
            $_charge['撤销流量数(L)'] = is_null($charge->discharge_flow)? '': $charge->discharge_flow;
            $_charge['撤销天数(天)'] = is_null($charge->discharge_time)? '': $charge->discharge_time;
            $_charge['撤销操作人'] = strval(is_null($charge->dischargeAdmin)? '': $charge->dischargeAdmin->name);
            $_charge['价格'] = strval(is_null($charge->price)? '': $charge->price);
            $_charge['商户订单号'] = strval(is_null($charge->order_number)? '': $charge->order_number);
            $_charge['备注信息'] = strval($charge->remark);
            $_charge['日期'] = strval($charge->created_at);
            foreach ($_charge as $key => $exportValue) {
                if(strpos($exportValue, '=') === 0){
                    $_charge[$key] = "'".$exportValue;
                }
            }
            array_push($formatChargeArray, $_charge);
        }
        
        (new ExportExcel)->export('充值记录', $formatChargeArray);
    }

    public function formatCondition($condition)
    {
        $condition = str_replace('%', '\%', $condition);
        $condition = str_replace('_', '\_', $condition);
        return $condition;
    }

    public function faults(Request $request)
    {
        $faults = DeviceService::getDeviceFaults($request->condition);
        return view("admin/deviceHitch")->withFaults($faults);
        
    }
    //净水 tds 提醒
    public function tdsRemindList(Request $request)
    {
        $condition = $this->formatCondition($request->condition);
        $remindRecords = DeviceService::findTdsRemindList($condition);
        return view("admin/remindersTds")->with([
            'remind_records' => $remindRecords,
            'condition' => $request->condition,
            ]);
    }
    //水机状态提醒
    public function statusRemindList(Request $request)
    {
        $condition = $this->formatCondition($request->condition);
        $remindRecords = DeviceService::findStatusRemindList($condition);
        return view("admin/remindersState")->with([
            'remind_records' => $remindRecords,
            'condition' => $request->condition,
            ]);
    }

}
